#!/bin/bash

set -eu

export PATH="$SNAP/usr/sbin:$SNAP/usr/bin:$SNAP/sbin:$SNAP/bin:$PATH"
ARCH="$($SNAP/bin/uname -m)"
export LD_LIBRARY_PATH="$SNAP/lib:$SNAP/usr/lib:$SNAP/lib/$ARCH-linux-gnu:$SNAP/usr/lib/$ARCH-linux-gnu"

if [ -e ${SNAP_DATA}/var/lock/clustered.lock ]
then
  echo "This MicroK8s deployment is acting as a node in a cluster. Please use the microk8s linkerd on the master."
  exit 0
fi

if [ ! -f "${SNAP_DATA}/bin/linkerd" ]; then
  echo "Linkerd not available, try enabling Linkerd. 'microk8s enable linkerd' or 'microk8s enable linkerd:--proxy-auto-inject' "
  exit 0
fi

source $SNAP/actions/common/utils.sh
ARCH=$(arch)

if echo "$*" | grep -v -q -- '--kubeconfig'; then
  exit_if_no_permissions
fi

exit_if_stopped
"${SNAP_DATA}/bin/linkerd" --kubeconfig="${SNAP_DATA}"/credentials/client.config "$@"